﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using JuCheap.Core.Interfaces;
using JuCheap.Core.Models;
using JuCheap.Core.Models.Enum;
using JuCheap.Core.Models.Filters;
using JuCheap.Core.Web.Filters;
using JuCheap.Core.Web.Models;
using JuCheap.Core.Infrastructure.Extentions;
using Microsoft.AspNetCore.Authorization;
using JuCheap.Core.Infrastructure.Attributes;
using JuCheap.Core.Infrastructure.Menu;
using JuCheap.Core.Models.ViewModels;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JuCheap.Core.Web.Controllers
{
    public class ShowPushController : Controller
    {
        private IShowPushService _ishowPushService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ishowPushService"></param>
        public ShowPushController(IShowPushService ishowPushService)
        {
            _ishowPushService = ishowPushService;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View(new ShowPushDto());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Add(ShowPushDto dto)
        {
            if (ModelState.IsValid)
            {
                var count = await _ishowPushService.GetPushCount();
                if (count == 0 || dto.Push == 0)
                {
                    var result = await _ishowPushService.AddAsync(dto);

                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError(nameof(dto.Push), "已有发布数据");
                }

            }
            return View(dto);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(int id)
        {
            var dto = await _ishowPushService.FindAsync(id);
            if (dto == null)
            {
                return RedirectToAction("Add");
            }
            return View(dto);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(ShowPushDto dto)
        {
            if (ModelState.IsValid)
            {
                var count = await _ishowPushService.GetPushCount();
                if (count == 0 || dto.Push == 0)
                {
                    var result = await _ishowPushService.UpdateAsync(dto);
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError(nameof(dto.Push), "已有发布数据");
                }
            }
            return View(dto);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Delete([FromBody]IEnumerable<string> ids)
        {
            var result = new JsonResultModel<bool>();
            if (ids.AnyOne())
            {
                result.flag = await _ishowPushService.DeleteAsync(ids);
            }
            return Json(result);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [IgnoreRightFilter]
        public async Task<IActionResult> GetListWithPager(ShouPushFilter filter)
        {
            var result = await _ishowPushService.SearchAsync(filter);
            return Json(result);
        }
    }
}
